<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>监视属性-天气</title>
    <script type="text/javascript" src="../../../public/vue.js"></script>
</head>
<body>
<div id="root">
    <!--
    监视属性watch:
        1.当被监视的属性变化时，回调函数自动调用进行相关操作
        2.监视的属性必须存在，才能进行监视!!
        3.监视的两种写法:
        (1).new Vue时传入watch配置
        (2).通过vm.$watch监视
    -->
    <h2>今天天气很{{ info }}</h2>
    <button @click="changeWeather">切换天气</button>
</div>
</body>
<script type="text/javascript">
    new Vue({
        el: '#root',
        data: {
            isHost: true
        },
        computed: {
            info: function () {
                return this.isHost ? '炎热' : '凉爽'
            }
        },
        methods: {
            changeWeather() {
                this.isHost = !this.isHost
            }
        },
        watch: {
            // 监视谁，怎么做
            isHost: {
                immediate: true,//立刻执行
                // 什么时候调用handler - isHost发生改变时，handler被调用
                handler(newVal, oldVal) {
                    console.log(newVal + "isHost被修改了" + oldVal)
                },
            }
        }
    })
</script>
</html>